// pages/reportList/reportDetail.js
var app = getApp();
var url = app.url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: "",
    info: "",
    id: '',
    imgurl: "",
    lngLat: "",
    blockShow: false,
    remark: "",
    remarkTitle:'维修备注',
    remarkContent:'',
    remarkShow:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    this.setData({
      id: options.id,
      dealwithRemark: options.dealwithRemark,
      userName: options.userName
    });
    this.setData({ imgurl: app.imageUrl })
    var that = this;
    var params = { id: id };
    wx.request({
      url: url + '/web/exceptionReport/getExceptionReportInfoById.htm',
      data: app.ecbEncrypto(params),
      method: 'POST',
      success: function (res) {
        var decodingStr = app.ecbDecrypto(res.data)
        var rest = JSON.parse(decodingStr);
        if (rest.isSuccess == 0) {
          var info = rest.result;
          info.createTime = new Date(info.createTime).Format("yyyy-MM-dd hh:mm:ss");
          info.dealTime = new Date(info.dealTime).Format("yyyy-MM-dd hh:mm:ss");
          info.imgList = info.imgUrl?info.imgUrl.split(","):[]
          info.imgUrlList = info.imgUrl?info.imgUrl.split(","):[]
          info.imgDealUrlList = info.imgDealUrl?info.imgDealUrl.split(","):[]
          info.additionalNote = info.additionalNote?JSON.parse(info.additionalNote):[]
          info.additionalNote.forEach(item=>{
            item.imgUrl = item.imgUrl?item.imgUrl.split(",").map(it=>that.data.imgurl + it):[]
          })
          info.forwardNote = info.forwardNote?JSON.parse(info.forwardNote):[];
          info.useTime = app.reduceTime(info.dealTime,info.createTime)
          that.setData({ info: info })
        } else {
          wx.showModal({
            title: '提示',
            content: rest.errorMsg,
            showCancel: false
          })
        }
        console.log(rest)
      },

    })
    console.log(id)

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件维修函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的维修函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //查看地图
  openMap: function (e) {
    var that = this;
    const lngLat = e.currentTarget.dataset.index;
    console.log(lngLat)
    const lng = +lngLat.split(',')[0];
    const lat = +lngLat.split(',')[1];
    if (lngLat != "") {
      if (this.data.info.reportType != 2 && this.data.info.reportType != 3) {
        wx.navigateTo({
          url: '../littleMap/littleMap?latitude=' + lat + '&longitude=' + lng + '&id=' + this.data.info.distributionBoxid,
        })
      } else {
        wx.openLocation({
          latitude: lat,
          longitude: lng,
          scale: 28
        })
      }
      // wx.navigateTo({
      //   url: '../littleMap/littleMap?latitude=' + that.data.latitude + '&longitude=' + that.data.longitude + '&id=' + this.data.info.distributionBoxid,
      // })
    }
  },
  callPhone: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone,
      success: function () {

      },
      fail: function () {

      }
    });
  },
  toHandle() {
    this.setData({ blockShow: true })
    // 
  },
  toDelete() {
    this.exsubmit()
  },
  exsubmit: function (type) {
    var id = this.data.id;
    var params = {
      reportId: id,
      userName: wx.getStorageSync('userName'),
      remark: this.data.remark,
      userId: wx.getStorageSync('userId'),
    };
    if (type) { params.type = 1 }
    console.log(params);
    wx.request({
      url: url + '/web/exceptionReport/wxdelById.htm',
      data: app.ecbEncrypto(params),
      method: 'POST',
      success: function (res) {
        var decodingStr = app.ecbDecrypto(res.data)
        var rest = JSON.parse(decodingStr);
        console.log(rest)
        if (rest.isSuccess == 0) {
          this.setData({
            blockShow: false
          })
          wx.navigateBack()
        } else {
          wx.showModal({
            title: '提示',
            content: rest.errorMsg,
            showCancel: false
          })
        }
      },

    })
  },
  //图预览
  img_tap: function (e) {
    var url = e.currentTarget.dataset.url;
    var pre_url = url
    wx.previewImage({
      current: pre_url, // 当前显示图片的http链接
      urls: [pre_url] // 需要预览的图片http链接列表
    })
  },
  bindKeyInput: function (e) {
    this.setData({
      remark: e.detail.value
    })
  },
  popupCancel() {
    this.setData({
      blockShow: false
    })
  },
  popupConfirm() {
    if (!this.data.remark) {
      wx.showModal({
        title: '提示',
        content: "请填写维修备注",
        showCancel: false
      })
      return
    }

    this.exsubmit(1)
  },
  openDialog(e){
    var data = e.currentTarget.dataset;
    if (data.content){
      this.setData({
        remarkTitle: data.title,
        remarkContent: data.content,
        remarkShow:true
      })
    }
  },
  closeRemark(){
    this.setData({
      remarkShow: false
    })
  }

})

Date.prototype.Format = function (fmt) { //author: meizz  
  var o = {
    "M+": this.getMonth() + 1, //月份  
    "d+": this.getDate(), //日  
    "h+": this.getHours(), //小时  
    "m+": this.getMinutes(), //分  
    "s+": this.getSeconds(), //秒  
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度  
    "S": this.getMilliseconds() //毫秒  
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
};